/*
 * @# shows.js 2011-12-28 上午09:36:31
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
/**
 * 秀场.
 */
$(function() {
	$(document).click(function(e) {
		$('#shows-settings-box').hide();
	});
	$('#shows-settings').click(function(e) {
		var $box = $('#shows-settings-box');
		if ($box.is(":hidden")) {
			$box.show();
		} else {
			$box.hide();
		}
		return false;
	});

	$('#edit-album').click(function(e) {
		var albumId = $('#album-id').val();
		EdibAlbumDialog.edit(albumId, function(album) {
			$('#album-name').text(album.name);
		});
		return false;
	});

	$('#remove-album').click(function(e) {
		var albumId = $('#album-id').val();
		$.post(window.ctx + '/albums/' + albumId + '/remove', function(feedback) {
			if (feedback.successful) {
				window.location.href = window.ctx + '/albums/shows';
			}
		});
		return false;
	});
	
	$('#upload-photos').click(function(e) {
		var album = {
			id : $('#album-id').val(),
			name : $('#album-name').text()
		}
		UploadPhotosDialog.createShows(album, function() {
			window.location.reload();
		});
	});
	
	var albumId = $('#album-id').val();
	var currentUserId = $('#current-user-id').val(), currentUserAvatar = $('#current-user-avatar').val();
	// 喜爱
	$.getJSON(window.ctx + '/albums/' + albumId + '/likes', function(likes) {
		var html = '';
		$.each(likes, function(i, like) {
			var user = like.user;
			html += '<a title="' + user.nickname + '" href="/u/' + user.id + '">' + 
						'<img src="' + Px.Avatar.getS40Url(user.avatar) + '" class="usercard">' +
					'</a>';
		});
		if (html) {
			$('#likes-user-list').html(html);
			$('#likes-content').removeClass('hidden').show();
		}
	});
	$('#like-album-btn').click(function(e) {
		$.post(window.ctx + '/albums/' + albumId + '/likes/create', function(feedback) {
			if (feedback.successful) {
				$('#like-album-text').text('已赞过');
				var num = parseInt($('#like-album-num').val() || 0);
				$('#like-album-num').val(num + 1);
				if (num < 20) {
					var html = '<a title="' + currentUserId + '" href="/u/"' + currentUserId + '>' + 
								'<img src="' + Px.Avatar.getS40Url(currentUserAvatar) + '" class="usercard">' +
							'</a>';
					$('#likes-user-list').append(html);
				}
			}
		});
	});
	// 收藏
	$('#favor-album-btn').click(function(e) {
		$.post(window.ctx + '/albums/' + albumId + '/favors/create', function(feedback) {
			if (feedback.successful) {
				$('#favor-album-text').text('已收藏');
				var num = parseInt($('#favor-album-num').val() || 0);
				$('#favor-album-num').val(num + 1);
			}
		});
	});
});
